package com.ruoyi.wetest.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.wetest.config.WeTestConfig;
import com.ruoyi.wetest.domain.WeTestTrans;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import com.ruoyi.wetest.mapper.WetestStressTransMapper;
import com.ruoyi.wetest.domain.WetestStressTrans;
import com.ruoyi.wetest.service.IWetestStressTransService;

/**
 * 压测上报事务记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-30
 */
@Service
public class WetestStressTransServiceImpl implements IWetestStressTransService {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final WetestStressTransMapper wetestStressTransMapper;

    private final WeTestConfig weTestConfig;

    public WetestStressTransServiceImpl(WeTestConfig weTestConfig, WetestStressTransMapper wetestStressTransMapper) {
        this.weTestConfig = weTestConfig;
        this.wetestStressTransMapper = wetestStressTransMapper;
    }

    @Override
    public void saveBatch(List<WeTestTrans> testTrans, String testId,int instanceId) {
        if (testTrans.isEmpty()||!weTestConfig.isEnabledRecordTrans()) return;
        long l = System.currentTimeMillis();
        ArrayList<WetestStressTrans> list = new ArrayList<>();
        for (WeTestTrans tt : testTrans) {
            WetestStressTrans trans = new WetestStressTrans();
            trans.setTransName(tt.getTrans_name());
            trans.setTransResult(tt.getTrans_result());
            trans.setTestId(testId);
            trans.setInstanceId(instanceId);
            trans.setTimeCost(tt.getTime_cost());
            trans.setTimestamps(tt.getTimestamps());
            trans.setcTime(l);
            list.add(trans);
        }
        wetestStressTransMapper.saveBatchWetestStressTrans(list);

    }

    /**
     * 查询压测上报事务记录
     * 
     * @param id 压测上报事务记录主键
     * @return 压测上报事务记录
     */
    @Override
    public WetestStressTrans selectWetestStressTransById(Long id)
    {
        return wetestStressTransMapper.selectWetestStressTransById(id);
    }

    /**
     * 查询压测上报事务记录列表
     * 
     * @param wetestStressTrans 压测上报事务记录
     * @return 压测上报事务记录
     */
    @Override
    public List<WetestStressTrans> selectWetestStressTransList(WetestStressTrans wetestStressTrans)
    {
        return wetestStressTransMapper.selectWetestStressTransList(wetestStressTrans);
    }

    /**
     * 新增压测上报事务记录
     * 
     * @param wetestStressTrans 压测上报事务记录
     * @return 结果
     */
    @Override
    public int insertWetestStressTrans(WetestStressTrans wetestStressTrans)
    {
        return wetestStressTransMapper.insertWetestStressTrans(wetestStressTrans);
    }

    /**
     * 修改压测上报事务记录
     * 
     * @param wetestStressTrans 压测上报事务记录
     * @return 结果
     */
    @Override
    public int updateWetestStressTrans(WetestStressTrans wetestStressTrans)
    {
        return wetestStressTransMapper.updateWetestStressTrans(wetestStressTrans);
    }

    /**
     * 批量删除压测上报事务记录
     * 
     * @param ids 需要删除的压测上报事务记录主键
     * @return 结果
     */
    @Override
    public int deleteWetestStressTransByIds(Long[] ids)
    {
        return wetestStressTransMapper.deleteWetestStressTransByIds(ids);
    }

    /**
     * 删除压测上报事务记录信息
     * 
     * @param id 压测上报事务记录主键
     * @return 结果
     */
    @Override
    public int deleteWetestStressTransById(Long id)
    {
        return wetestStressTransMapper.deleteWetestStressTransById(id);
    }
}
